package com.xique.api.controller;

import cn.hutool.core.util.PhoneUtil;
import com.xique.common.core.constant.Constants;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.park.api.RemoteParkCarService;
import com.xique.park.api.request.RemoteBlackCarRequest;
import com.xique.park.api.request.RemoteGetCarListRequest;
import com.xique.park.api.request.RemoteParkCarAddRequest;
import com.xique.park.api.request.RemoteParkCarEditRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author shd
 * @create 2022/6/17 13:53
 */
@RestController
@RequestMapping("/park/v1")
@Slf4j
public class ParkV1Controller extends BaseController {

    @Autowired
    private RemoteParkCarService remoteParkCarService;

    /**
     * 新增车辆
     */
    @PostMapping("/addCar")
    public AjaxResult addCar(@RequestBody @Validated RemoteParkCarAddRequest request) {
        request.setParkId(SecurityUtils.getForeignId());
        //默认部分车库统计
        request.setIsGarageSpace("N");
        //状态默认正常
        request.setStatus("normal");
        //支付方式-其他
        request.setPayType("10");
        request.setMayFee(1);
        request.setActualFee(1);
        if (StringUtils.isNotEmpty(request.getPhone()) &&  !PhoneUtil.isPhone(request.getPhone())){
            return AjaxResult.error("手机号格式不正确");
        }

        request.setIsUserUpdate(request.getUserId() != null ? "Y" : "N");
        AjaxResult result = remoteParkCarService.addCar(request);
        return result;
    }


    /**
     * 修改车辆
     */
    @PutMapping("/updateCar")
    public AjaxResult updateCar(@RequestBody @Validated RemoteParkCarEditRequest request) {
        request.setParkId(SecurityUtils.getForeignId());
        //默认部分车库统计
        request.setIsGarageSpace("N");
        //状态默认正常
        request.setStatus("normal");

        if (StringUtils.isNotEmpty(request.getPhone()) &&  !PhoneUtil.isPhone(request.getPhone())){
            return AjaxResult.error("手机号格式不正确");
        }

        AjaxResult result = remoteParkCarService.updateCar(request);
        return result;
    }

    /**
     * 删除车辆
     */
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult removeCar(@RequestParam("carId") Long carId) {
        AjaxResult result = remoteParkCarService.removeCar(carId, SecurityUtils.getForeignId());
        return result;
    }

    /**
     * 获取车辆列表
     */
    @PostMapping("/getCarList")
    public AjaxResult getCarList(@Valid @RequestBody RemoteGetCarListRequest request) {
        request.setParkId(SecurityUtils.getForeignId());
        return remoteParkCarService.getCarList(request);
    }

    /**
     * 获取车辆管理类型列表
     */
    @GetMapping("/getMngTypeList")
    public AjaxResult getMngTypeList() {
        return remoteParkCarService.getMngTypeList(SecurityUtils.getForeignId());
    }

    /**
     * 拉黑
     */
    @PostMapping("/addBlack")
    public AjaxResult addBlack(@Valid @RequestBody RemoteBlackCarRequest request) {
        request.setParkId(SecurityUtils.getForeignId());
        request.setBeginTime(Constants.FOREVER_STR);
        request.setEndTime(Constants.FOREVER_STR);
        request.setRemark("拉黑");
        return remoteParkCarService.addBlack(request);
    }

    /**
     * 移除拉黑（会删除车辆信息）
     */
    @DeleteMapping("/removeBlack")
    public AjaxResult removeBlack(Long carId) {
        if (StringUtils.isNull(carId)) {
            return AjaxResult.error("车辆id不能为空");
        }
        return remoteParkCarService.removeBlack(carId, SecurityUtils.getForeignId());
    }
}
